package com.zu.commu.saveData;

import com.zu.commu.context.ServerContext;
import com.zu.commu.interf.Data;
import com.zu.commu.interf.saveData.SaveData;
import org.apache.log4j.Logger;

import java.util.ArrayList;

public class SaveDataWorker
{
  private static DataQueue dataQueue;
  private static Thread thread;
  private static ArrayList<Thread> threads;
  private static SaveDataWorker me;

  private SaveDataWorker(DataQueue dq)
  {
    dataQueue = dq;
    thread = new saveFirstDataThread();
    thread.start();

    threads = new ArrayList();
    int num = ServerContext.saveDataThreadNum < 2 ? 2 : ServerContext.saveDataThreadNum;
    for (int i = 1; i < num; i++) {
      Thread t = new saveSecondDataThread();
      threads.add(t);
      t.start();
    }
  }

  public static SaveDataWorker instance(DataQueue dq)
  {
    if (me == null) {
      me = new SaveDataWorker(dq);
    }
    return me;
  }

  private class saveFirstDataThread extends Thread
  {
    saveFirstDataThread()
    {
    }

    public void run()
    {
      String insertClassName = ServerContext.saveDataClass;
      Class c = null;
      try {
        c = Class.forName(insertClassName);
      } catch (Exception e) {
        try {
          c = Class.forName(insertClassName);
        } catch (Exception ee) {
          Logger log = Logger.getLogger("com.automic.commu.saveData.saveFirstDataThread");
          log.error("没有得到存储数据的类！", ee);
        }
      }
      while (true) try {
          Data d = SaveDataWorker.dataQueue.getDataFromFirstQueue();
          SaveData saveData = null;
          if (c != null) {
            Object o = c.newInstance();
            if (o != null) {
              saveData = (SaveData)o;
            }
          }
          saveData.insertData(d);
        } catch (Exception e) {
          Logger log = Logger.getLogger("com.automic.commu.saveData.saveFirstDataThread");
          log.error("从数据缓存中取得命令结果数据发生等待异常！", e);
        }
    }
  }

  private class saveSecondDataThread extends Thread
  {
    saveSecondDataThread()
    {
    }

    public void run()
    {
      String insertClassName = ServerContext.saveDataClass;
      Class c = null;
      try {
        c = Class.forName(insertClassName);
      } catch (Exception e) {
        try {
          c = Class.forName(insertClassName);
        } catch (Exception ee) {
          Logger log = Logger.getLogger("com.automic.commu.saveData.saveSecondDataThread");
          log.error("没有得到存储数据的类！", ee);
        }
      }
      while (true) try {
          Data d = SaveDataWorker.dataQueue.getDataFromSecondQueue();
          SaveData saveData = null;
          if (c != null) {
            Object o = c.newInstance();
            if (o != null) {
              saveData = (SaveData)o;
            }
          }
          saveData.insertData(d);
        } catch (Exception e) {
          Logger log = 
            Logger.getLogger("com.automic.commu.saveData.saveSecondDataThread");
          log.error("从数据缓存中取得主动上报数据发生等待异常！" + e.getMessage(), e);
        }
    }
  }
}